package thread;

public class Demo14 {
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
            synchronized (locker1){
                System.out.println("t1拿到第一把锁");

                //如果不加sleep，可能t1线程都执行完了，t2再执行
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2){
                    System.out.println("t1尝试拿到第二把锁");
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (locker2){
                System.out.println("t2拿到第二把锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker1){
                    System.out.println("t2尝试拿到第一把锁");
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }
}